<?php

class Rapide_Cache_Driver_Memcache implements Rapide_Cache_Driver_Interface
{

	private $_oMemCache,
					$_sCacheName,
					$_iLifeTime,
					$_bCompression;

	public function __construct($sCacheName, $sGroupName, $iLifeTime)
	{
		if (!extension_loaded('memcache'))
		{
			throw new Rapide_Cache_Exception('Memcache extension does not exist.');
		}

		$this->_oConfig = new Rapide_Config;
		$this->_oConfig->load('Cache/Memcache.php');

		$this->_bCompression = (bool)$this->_oConfig->get('Cache', 'Memcache', 'compression');

		$this->_oMemCache = new Memcache;

		$this->_oMemCache->addServer($this->_oConfig->get('Cache', 'Memcache', 'host'),
																 $this->_oConfig->get('Cache', 'Memcache', 'port'),
																 $this->_oConfig->get('Cache', 'Memcache', 'persistent'));

		$this->_sCacheName = $sCacheName;
		$this->_iLifeTime = $iLifeTime;
	}

	public function save($mData)
	{

		$this->_bCompression ? $iFlag = MEMCACHE_COMPRESSED : $iFlag = 0;

		return $this->_oMemCache->set($this->_sCacheName,
																	array($mData, time()),
																	$iFlag,
																	$this->_iLifeTime);
	}

	public function load()
	{
		$aData = $this->_oMemCache->get($this->_sCacheName);
		if (is_array($aData))
		{
				return $aData[0];
		}
				return false;

	}


	public function checkIfNotExpired()
	{
		return;
	}

	public function ping()
	{
		$aData = $this->_oMemCache->get($this->_sCacheName);
		if (is_array($aData))
		{
				return $aData[1];
		}
				return false;
	}

	public function clearAll()
	{
		return $this->_oMemCache->flush();
	}

	public function clearGroup($sGroupName)
	{
		throw new Rapide_Cache_Exception('Memcache does not support this method.');
	}

	public function clearOld()
	{
		throw new Rapide_Cache_Exception('Memcache does not support this method.');
	}

	public function clearOldFromGroup($sGroupName)
	{
		throw new Rapide_Cache_Exception('Memcache does not support this method.');
	}

	public function remove()
	{
		return $this->_oMemCache->delete($this->_sCacheName);
	}
}

?>